package com.mqz.zebraEcommerce.commodity.service.impl;

import com.mqz.zebraEcommerce.commodity.entity.AttrEntity;
import com.mqz.zebraEcommerce.commodity.service.AttrService;
import com.mqz.zebraEcommerce.commodity.vo.AttrGroupAndAttrsVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mqz.zebraEcommerce.common.utils.PageUtils;
import com.mqz.zebraEcommerce.common.utils.Query;

import com.mqz.zebraEcommerce.commodity.dao.AttrGroupDao;
import com.mqz.zebraEcommerce.commodity.entity.AttrGroupEntity;
import com.mqz.zebraEcommerce.commodity.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(key)) {
//            wrapper.eq("id", key).or().like("name", key);
            wrapper.and(obj -> {
                obj.eq("id", key).or().like("name", key);
            });
        }
        //0表示无检索条件
        if (categoryId != 0) {
            wrapper.eq("category_id", categoryId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Resource
    private AttrService attrService;

    //流式计算将attrGroup和attr封装到VO对象返回
    @Override
    public List<AttrGroupAndAttrsVO> getAttrGroupAndAttrsVOByCategoryId(Long categoryId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("category_id", categoryId));
        List<AttrGroupAndAttrsVO> attrGroupAndAttrsVOs = attrGroupEntities.stream().map(attrGroupEntity -> {
            AttrGroupAndAttrsVO attrGroupAndAttrsVO = new AttrGroupAndAttrsVO();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupAndAttrsVO);

            List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroupAndAttrsVO.getId());
            attrGroupAndAttrsVO.setAttrs(attrEntities);
            return attrGroupAndAttrsVO;
        }).collect(Collectors.toList());
        return attrGroupAndAttrsVOs;
    }
}